<!-- 显示任务详情，提供 “标记为已完成”“删除任务”“返回首页” 按钮 -->
<template>
  <div class="pending-tasks">
    <div class="back-button-container">
      <button @click="goBackToHome" class="back-button">
        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-left" viewBox="0 0 16 16">
          <path fill-rule="evenodd" d="M15 8a.5.5 0 0 0-.5-.5H2.707l3.147-3.146a.5.5 0 1 0-.708-.708l-4 4a.5.5 0 0 0 0 .708l4 4a.5.5 0 0 0 .708-.708L2.707 8.5H14.5A.5.5 0 0 0 15 8z"/>
        </svg>
        返回首页
      </button>
    </div>
    
    <h2>我的待办任务</h2>
    
    <TaskForm @taskAdded="addNewTask" />
    
    <div v-if="pendingTasks.length === 0" class="empty-state">
      <p>暂无待办任务，点击上方添加新任务</p>
    </div>
    
    <div v-else>
      <div class="task-list">
        <!-- 关键修复点：确保TaskCard正确传递task和事件 -->
        <TaskCard 
          v-for="task in pendingTasks" 
          :key="task.id" 
          :task="task" 
          @taskToggled="toggleTaskStatus"
          @taskDeleted="deleteTask"
          @taskEdited="editTask"
        />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { useTaskStore } from '../store/taskStore';
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import TaskForm from '../components/TaskForm.vue';
import TaskCard from '../components/TaskCard.vue';

const taskStore = useTaskStore();
const router = useRouter();

// 从Store获取响应式待办任务列表
const pendingTasks = taskStore.pendingTasks;

const addNewTask = (task: {
  title: string, 
  description: string, 
  dueDate: string
}) => {
  taskStore.addTask(task);
};

// 处理任务状态切换
const toggleTaskStatus = (id: string) => {
  taskStore.toggleTaskStatus(id);
};

const deleteTask = (id: string) => {
  taskStore.deleteTask(id);
};

const editTask = (taskId: string) => {
  router.push(`/task/${taskId}`);
};

const goBackToHome = () => {
  router.push('/');
};
</script>
<!-- 显示任务详情，提供 “标记为已完成”“删除任务”“返回首页” 按钮 -->
<style scoped>
.pending-tasks {
  margin-top: 2rem;
}

h2 {
  margin-bottom: 1rem;
  color: #4a6cf7;
}

.empty-state {
  padding: 2rem;
  text-align: center;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.task-list {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  margin-top: 1rem;
}

.back-button-container {
  margin-bottom: 1.5rem;
}

.back-button {
  display: flex;
  align-items: center;
  padding: 0.5rem 1rem;
  background-color: #f0f2ff;
  color: #4a6cf7;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.875rem;
  transition: background-color 0.3s;
}

.back-button svg {
  margin-right: 0.5rem;
}

.back-button:hover {
  background-color: #e0e6ff;
}
</style>